import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:huoxingwen/global.dart';
import 'package:huoxingwen/http/api.dart';
import 'package:huoxingwen/toast.dart';

class DetailPage extends StatefulWidget {
  DetailPage({Key key}) : super(key: key);
  @override
  _DetailPageState createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  @override
  void initState() {
    super.initState();
  }

  search(type, text) {
    if (text.trim() == '') {
      Toast.show(context, msg: '请输入要转换的文本', duration: 1);
      return false;
    }
    if (text.trim().length > 40) {
      Toast.show(context, msg: '原文不得超过40个字', duration: 1);
      return false;
    }
    if (text != null && text.trim() != '') {
      EasyLoading.show(status: '转换中...');
      transform(type, text.trim()).then((res) {
        EasyLoading.dismiss();
        if (res == null || res['text'] == null) {
          EasyLoading.showToast('转换失败，再试一次吧');
        }
        setState(() {
          _encryptText = res['text'];
        });
      });
    }
  }

  String _originText = '';
  String _encryptText = '';
  TextEditingController _encryptTextController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    _encryptTextController.text = _encryptText;
    return Scaffold(
      appBar: AppBar(
        title: Text('火星文转换', style: TextStyle(fontSize: 14)),
        elevation: 0,
        backgroundColor: Global.backgroundColor,
      ),
      body: FlutterEasyLoading(
        child: Container(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          color: Global.backgroundColor,
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  margin: EdgeInsets.all(15),
                  padding: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    color: Global.fontColor.withOpacity(.2),
                    border: Border.all(
                      color: Global.fontColor.withOpacity(.5),
                    ),
                    borderRadius: BorderRadius.circular(5),
                  ),
                  child: ConstrainedBox(
                    constraints: BoxConstraints(
                      maxWidth: MediaQuery.of(context).size.width,
                    ),
                    child: TextField(
                      maxLines: 7,
                      style: TextStyle(
                        color: Global.fontColor,
                        fontSize: 14,
                      ),
                      decoration: InputDecoration.collapsed(
                        hintText: '输入要转为火星文或者简体的原文，点击下方按钮进行转换。',
                        hintStyle: TextStyle(
                          color: Global.fontSecondColor.withOpacity(0.5),
                          fontSize: 14,
                        ),
                      ),
                      onChanged: (text) {
                        setState(() {
                          _originText = text;
                        });
                      },
                    ),
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    RaisedButton(
                      color: Global.primaryColor,
                      elevation: 0,
                      child: Text(
                        '转为火星文',
                        style: TextStyle(
                          color: Global.backgroundColor,
                          fontSize: 12,
                        ),
                      ),
                      onPressed: () {
                        search(2, _originText);
                      },
                    ),
                    RaisedButton(
                      color: Global.primaryColor,
                      child: Text(
                        '转为简体',
                        style: TextStyle(
                          color: Global.backgroundColor,
                          fontSize: 12,
                        ),
                      ),
                      onPressed: () {
                        search(0, _originText);
                      },
                    ),
                    RaisedButton(
                      color: Global.primaryColor,
                      child: Text(
                        '转为繁体',
                        style: TextStyle(
                          color: Global.backgroundColor,
                          fontSize: 12,
                        ),
                      ),
                      onPressed: () {
                        search(1, _originText);
                      },
                    ),
                  ],
                ),
                Container(
                  margin: EdgeInsets.all(15),
                  padding: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    color: Global.fontColor.withOpacity(.07),
                    border: Border.all(
                      color: Global.fontColor.withOpacity(.15),
                    ),
                    borderRadius: BorderRadius.circular(5),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      ConstrainedBox(
                        constraints: BoxConstraints(
                          maxWidth: MediaQuery.of(context).size.width,
                        ),
                        child: TextField(
                          maxLines: 15,
                          readOnly: true,
                          controller: _encryptTextController,
                          style: TextStyle(
                            color: Global.fontColor,
                            fontSize: 14,
                          ),
                          decoration: InputDecoration.collapsed(
                            hintText: '转换后的文本会在这里显示',
                            hintStyle: TextStyle(
                              color: Global.fontSecondColor.withOpacity(0.5),
                              fontSize: 14,
                            ),
                          ),
                        ),
                      ),
                      RaisedButton(
                        color: Global.primaryColor,
                        elevation: 5,
                        child: Text(
                          '复制文本',
                          style: TextStyle(
                            color: Global.backgroundColor,
                            fontSize: 12,
                          ),
                        ),
                        onPressed: () {
                          Toast.show(context, msg: '已复制到剪贴板', duration: 1);
                          Clipboard.setData(
                              ClipboardData(text: _encryptTextController.text));
                        },
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
